home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cgazv5n4.arc / METAFILE.C < prev    next >
C/C++ Source or Header  |  1991-09-23  |  4KB  |  127 lines

  1. /*--- Listing 2 -------------------------- METAFILE.C -----------
  2. * Author:  Gary A. Parker
  3. * Last update: 3-12-91
  4. * Compilers: Turbo C++ 1.0, MSC 6.0
  5. * Memory model: any
  6. *
  7. * A demonstration program for the metaphone().
  8. * The program will search a drive for a filename
  9. * which sounds similar to the name passed.
  10. *
  11. * This code is hereby placed in the public domain.
  12. *--------------------------------------------------------------*/
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <dos.h>
  18.  
  19. #if defined(__TURBOC__)          /* Turboc C */
  20.     #include <dir.h>
  21. typedef struct ffblk   FILEINFO;
  22.     #define F_FIRST(a,b,c) findfirst(a,c,b)
  23.     #define F_NEXT         findnext
  24.     #define F_ATTRIB       ff_attrib
  25.     #define F_DATE         ff_fdate
  26.     #define F_SIZE         ff_fsize
  27.     #define F_NAME         ff_name
  28. #else
  29. typedef struct find_t  FILEINFO; /* MSC      */
  30.     #define F_FIRST        _dos_findfirst
  31.     #define F_NEXT         _dos_findnext
  32.     #define F_ATTRIB       attrib
  33.     #define F_DATE         wr_date
  34.     #define F_SIZE         size
  35.     #define F_NAME         name
  36. #endif
  37.  
  38. /* file attributes */
  39. #define RDONLY   0x01 /* Read only file */
  40. #define HIDDEN   0x02 /* Hidden file */
  41. #define SYSTEM   0x04 /* System file */
  42. #define SUBDIR   0x10 /* Subdirectory */
  43. #define ARCH     0x20 /* Archive file */
  44.  
  45. /* display the file and path if it matches  */
  46. void print_find_t(char *dir, FILEINFO *find);
  47.  
  48. /* perform metaphone comparison */
  49. int metaphone(char *name, char *m, int first);
  50.  
  51. /* search dos file system recursively */
  52. void search(char *dir, char *name);
  53.  
  54. int found = 0;
  55. char meta[8]; /* storage for first metaph */
  56.  
  57. main(int argc, char *argv[])
  58. {
  59.     char *ptr;
  60.  
  61.     if(argc == 1) {
  62.         puts("syntax metaph [filename]");
  63.         exit(1);
  64.     }
  65.     /* remove extension if any */
  66.     if((ptr = memchr(argv[1], '.', 9)) != 0) *ptr = 0;
  67.     metaphone(argv[1], meta, 1); /* store the metaph */
  68.     printf("\nmetaph for %s is %s\n", argv[1], meta);
  69.     search("", "");              /* search for matchs */
  70.     if(!found) printf("A match was not found\n");
  71.     return 0;
  72. }
  73.  
  74. void search(char *dir, char *newdir)
  75. {
  76.     char curdir[80];
  77.     FILEINFO find;
  78.     char *ptr;
  79.  
  80.     strcpy(curdir, dir); /* build first pathname */
  81.     strcat(curdir, newdir);
  82.     strcat(curdir, "\\*.*");
  83.  
  84.     /* check first file */
  85.     if( !F_FIRST( curdir, 0xFF, &find) ) {
  86.         curdir[strlen(curdir) - 3] = 0; /* remove *.* */
  87.         do  {
  88.             if(find.F_NAME[0] == '.') continue;
  89.             else{                       /* skip extension */
  90.                 if((ptr = memchr(find.F_NAME, '.', 9)) != 0)
  91.                     *ptr = 0;
  92.  
  93.                 /* call metaphone with flag set to
  94.                    0 to compare string with stored
  95.                    metaph */
  96.  
  97.                 if(metaphone(find.F_NAME, meta, 0) == 0) { 
  98.                     /* put back extension */
  99.                     if(ptr != 0) *ptr = '.';
  100.                     print_find_t(curdir, &find);
  101.                 }
  102.             }
  103.  
  104.             if((find.F_ATTRIB & SUBDIR))
  105.                 search(curdir, find.F_NAME); /* recursion */
  106.         } while( !F_NEXT( &find ) );
  107.     }
  108. }
  109.  
  110. /* display the file information */
  111. void print_find_t(char *dir, FILEINFO *find)
  112. {
  113.     printf("%s%-12s %8ld %2.2d-%02.2d-%02.2d %c%c%c%c%c\r\n",
  114.         dir, find->F_NAME, find->F_SIZE,
  115.         (find->F_DATE >> 5) & 0x0f,
  116.         find->F_DATE & 0x1f,
  117.         ((find->F_DATE >> 9) + 80) % 100,
  118.         (find->F_ATTRIB & SUBDIR) ? 'D' : '.',
  119.         (find->F_ATTRIB & RDONLY) ? 'R' : '.',
  120.         (find->F_ATTRIB & HIDDEN) ? 'H' : '.',
  121.         (find->F_ATTRIB & SYSTEM) ? 'S' : '.',
  122.         (find->F_ATTRIB & ARCH)   ? 'A' : '.' );
  123.  
  124.     found = 1;
  125. }
  126.